<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>主页</title>
    <!-- 依 赖 样 式 -->
    <link rel="stylesheet" th:href="@{/pear/component/pear/css/pear.css}" type="text/css"/>
    <!-- 加 载 样 式 -->
    <link rel="stylesheet" th:href="@{/pear/admin/css/loader.css}"/>
    <!-- 布 局 样 式 -->
    <link rel="stylesheet" th:href="@{/pear/admin/css/admin.css}"/>
    <link rel="stylesheet" th:href="@{/pear/admin/css/admin.dark.css}"/>
    <link rel="stylesheet" th:href="@{/pear/admin/css/variables.css}"/>
    <link rel="stylesheet" th:href="@{/pear/admin/css/reset.css}"/>
</head>
<style>
    /* 修改鼠标悬浮时的指针样式 */
    .layui-nav-child dd:hover {
        cursor: pointer; /* 鼠标指针显示为手形 */
    }
</style>
<!-- 结 构 代 码 -->
<body class="layui-layout-body pear-admin">
<!-- 布 局 框 架 -->
<div class="layui-layout layui-layout-admin">
    <!-- 顶 部 样 式 -->
    <div class="layui-header">
        <!-- 菜 单 顶 部 -->
        <div class="layui-logo">
            <!-- 图 标 -->
            <img class="logo">
            <!-- 标 题 -->
            <span class="title"></span>
        </div>
        <!-- 顶 部 左 侧 功 能 -->
        <ul class="layui-nav layui-layout-left">
            <li class="collapse layui-nav-item"><a href="#" class="layui-icon layui-icon-shrink-right"></a></li>
            <li class="refresh layui-nav-item"><a href="#" class="layui-icon layui-icon-refresh-1" loading=600></a></li>
        </ul>
        <!-- 多 系 统 菜 单 -->
        <div id="control" class="layui-layout-control"></div>
        <!-- 顶 部 右 侧 菜 单 -->

        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item">
                <a href="javascript:;" class="layui-icon layui-icon-group"></a>
                <dl class="layui-nav-child">
                    <dd>
                        <select name="department" id="department" lay-filter="department" class="layui-select">
                            <!-- 动态加载部门选项 -->
                        </select>
                    </dd>
                </dl>
            </li>
            <li class="layui-nav-item layui-hide-xs"><a href="#" class="menuSearch layui-icon layui-icon-search"></a>
            </li>
            <li class="layui-nav-item layui-hide-xs"><a href="#"
                                                        class="fullScreen layui-icon layui-icon-screen-full"></a></li>
            <li class="layui-nav-item layui-hide-xs"><a href="http://www.baidu.com"
                                                        class="layui-icon layui-icon-website"></a></li>
            <li class="layui-nav-item user">
                <!-- 头 像 -->
                <a class="layui-icon layui-icon-username" href="javascript:"></a>
                <!-- 功 能 菜 单 -->
                <dl class="layui-nav-child">
                    <dd><a user-menu-url="common/system/person" user-menu-id="5555"
                           user-menu-title="基本资料">基本资料</a>
                    </dd>
                    <dd><a user-menu-url="common/system/editPwd" user-menu-id="6666"
                           user-menu-title="修改密码">修改密码</a>
                    </dd>
                    <dd><a href="javascript:void(0);" class="logout">注销登录</a></dd>
                </dl>
            </li>
            <!-- 主 题 配 置 -->
            <li class="layui-nav-item setting"><a href="#" class="layui-icon layui-icon-more-vertical"></a></li>
        </ul>
    </div>
    <!-- 侧 边 区 域 -->
    <div class="layui-side layui-bg-black">
        <!-- 菜 单 顶 部 -->
        <div class="layui-logo">
            <!-- 图 标 -->
            <img class="logo">
            <!-- 标 题 -->
            <span class="title"></span>
        </div>
        <!-- 菜 单 内 容 -->
        <div class="layui-side-scroll">
            <div id="side"></div>
        </div>
    </div>
    <!-- 视 图 页 面 -->
    <div class="layui-body">
        <!-- 内 容 页 面 -->
        <div id="content" style="width: 100%; height: 100%; position: relative;"></div>
    </div>
    <!-- 页脚 -->
    <div class="layui-footer layui-text">
				<span class="left">
					Released under the MIT license.
				</span>
        <span class="center"></span>
        <span class="right">
					Copyright © 2021-2022 pearadmin.com
				</span>
    </div>
    <!-- 遮 盖 层 -->
    <div class="pear-cover"></div>
    <!-- 加 载 动 画 -->
    <div class="loader-wrapper">
        <!-- 动 画 对 象 -->
        <div class="loader"></div>
    </div>
</div>
<!-- 移 动 端 便 捷 操 作 -->
<div class="pear-collapsed-pe collapse">
    <a href="#" class="layui-icon layui-icon-shrink-right"></a>
</div>
<!-- 依 赖 脚 本 -->
<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>
<!-- 框 架 初 始 化 -->
<script th:inline="javascript">
    layui.use(['admin', 'jquery', 'popup'], function () {
        let baseURL = /*[[@{/}]]*/ '';
        let $ = layui.jquery;
        let admin = layui.admin;
        let popup = layui.popup;
        let selectedDepartmentId;

        // admin.setConfigType("json");
        // admin.setConfigPath(baseURL + "config");
        admin.setConfigurationPath(baseURL + "config")
        // admin.setConfigurationPath(baseURL + "pear/config/pear.config.yml");
        admin.render();
        // 定义 window.hl 指向 sideMenu 容器
        // window.hl = $("#sideMenu");


        // window.addEventListener('message', function (event) {
        //     if (event.data.type === 'updateMenu') {
        //         admin.menuRender(event.data.data); // 调用 admin 的 menuRender 方法更新菜单
        //     }
        // }, false);
        // 登出逻辑
        admin.logout(function () {
            popup.success("注销成功", function () {
                location.href = "login";
            })
            // 注销逻辑 返回 true / false
            return true;
        })

        // 定义一个方法，用于将部门ID存储到Session中
        function setDepartmentIdToSession(departmentId) {
            $.ajax({
                url: baseURL + 'department/session/setDepartmentId', // 后端接口地址
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({departmentId: departmentId}), // 发送的 JSON 数据
                success: function (response) {
                    console.log(response)
                    if (response.code === 1000) {
                        console.log('部门ID已成功存储到Session');
                    } else {
                        layer.msg('存储部门ID到Session失败', {icon: 2});
                    }
                },
                error: function () {
                    layer.msg('存储部门ID到Session失败', {icon: 2});
                }
            });
        }

// 加载部门信息
        $.ajax({
            url: baseURL + 'department/departmentByUserId', // 后端接口地址
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                var select = $('#department');
                let department = data.data;

                // 清空下拉框并添加选项
                select.empty().append('<option value="">请选择部门</option>');
                $.each(department, function (index, dept) {
                    select.append('<option value="' + dept.departmentId + '">' + dept.departmentName + '</option>');
                });
                select.on('change', function () {
                    selectedDepartmentId = $(this).val();
                    setDepartmentIdToSession(selectedDepartmentId);
                    window.location.reload();
                });
            },
            error: function () {
                layer.msg('加载部门信息失败', {icon: 2});
            }
        });

        // // 消息点击回调
        // admin.message(function (id, title, context, form) {
        // });

        $(document).ready(function () {

            /*<![CDATA[*/
            const passwordExpired = /*[[${session.passwordExpired}]]*/ false;
            /*]]>*/
            console.log(passwordExpired)
            if (passwordExpired) {
                $.ajax({
                    url: baseURL + 'user/check-and-handle-password-expiration',
                    method: 'POST',
                    success: function (response) {
                        if (response.code === 2010) { // 假设 ACCOUNT_LOCKED 的 code 是 403
                            layer.alert('账户已被锁定，请联系管理员！', {
                                icon: 3,
                                skin: 'layui-layer-molv',
                                closeBtn: 0,
                                shadeClose: true,
                                btn: ['确定'],
                                yes: function (index, layero) {
                                    location.href = "./login";
                                    layer.close(index);
                                },
                                end: function () {
                                    location.href = "./login";
                                }
                            });
                        } else if (response.code === 2011) { // 假设 PASSWORD_EXPIRED 的 code 是 400
                            layer.alert('您的密码已过期，请及时修改密码！', {
                                icon: 3,
                                skin: 'layui-layer-molv',
                                closeBtn: 0,
                                shadeClose: true,
                                btn: ['立即修改'],
                                yes: function (index, layero) {
                                    location.href = "./common/system/editPwd";
                                    layer.close(index);
                                }
                            });
                        } else if (response.code === 2013) {
                            layer.alert('这是最后一次修改密码的机会，下一次将被锁定！', {
                                icon: 3,
                                skin: 'layui-layer-molv',
                                closeBtn: 0,
                                shadeClose: true,
                                btn: ['立即修改'],
                                yes: function (index, layero) {
                                    location.href = "./common/system/editPwd";
                                    layer.close(index);
                                }
                            });
                        }
                    },
                    error: function (xhr, status, error) {
                        console.error("Error checking and handling password expiration:", error);
                    }
                });
            }
        });
    })
</script>
</body>
</html>
